#ifndef CONSOLELOGRECEIVER_HPP
#define CONSOLELOGRECEIVER_HPP

#include "ILogReceiver.hpp"
#include <QDebug>
#include <QDateTime>

/**
 * @brief ConsoleLogReceiver 类用于将日志消息打印到控制台
 */
class ConsoleLogReceiver : public ILogReceiver {
public:


    void writeLog(QtMsgType type, const QString &file, int line, const QString &function, const QString &msg) override {
        // 仅处理 DEBUG 级别的日志
        if (type != QtDebugMsg) {
            return;
        }

        QString logLevel = "DEBUG";
        QString timestamp = QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss.zzz");
        QString formattedMessage = QString("[%1] [%2] %3:%4 (%5) - %6")
                                   .arg(timestamp, logLevel, file, QString::number(line), function, msg);

        // 使用 qDebug 打印日志
        qDebug().noquote() << formattedMessage;
    }

    // void writeLog(QtMsgType type, const QString &file, int line, const QString &function, const QString &msg) override {
    //     QString logLevel;
    //     switch (type) {
    //     case QtDebugMsg:
    //         logLevel = "DEBUG";
    //         break;
    //     case QtInfoMsg:
    //         logLevel = "INFO";
    //         break;
    //     case QtWarningMsg:
    //         logLevel = "WARNING";
    //         break;
    //     case QtCriticalMsg:
    //         logLevel = "CRITICAL";
    //         break;
    //     case QtFatalMsg:
    //         logLevel = "FATAL";
    //         break;
    //     default:
    //         logLevel = "UNKNOWN";
    //         break;
    //     }

    //     QString timestamp = QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss.zzz");
    //     QString formattedMessage = QString("[%1] [%2] %3:%4 (%5) - %6")
    //                                .arg(timestamp, logLevel, file, QString::number(line), function, msg);

    //     // 根据日志级别选择适当的输出函数
    //     switch (type) {
    //     case QtDebugMsg:
    //         qDebug().noquote() << formattedMessage;
    //         break;
    //     case QtInfoMsg:
    //         qInfo().noquote() << formattedMessage;
    //         break;
    //     case QtWarningMsg:
    //         qWarning().noquote() << formattedMessage;
    //         break;
    //     case QtCriticalMsg:
    //         qCritical().noquote() << formattedMessage;
    //         break;
    //     case QtFatalMsg:
    //         qFatal("%s", qPrintable(formattedMessage));
    //         break;
    //     default:
    //         qDebug().noquote() << formattedMessage;
    //         break;
    //     }
    // }
};

#endif // CONSOLELOGRECEIVER_HPP
